###########################################################################
#rtl生成
#
#修改时间：2021/09/18
###########################################################################
import csv
import ast


class rtl_generate():
	def __init__(self):
		pass

	def fir_filter_generate(self):
		input_def = f'\t\tinput signed [length-1:0] W'
		input_all = f''
		for i in range(32):
			input_all = input_all + f'{input_def}{i},\n'

		output_all = f''
		for i in range(32):
			output_all = output_all + f'\t\toutput signed [length-1:0] Mult_result{i},\n'

		Mult = f''
		for i in range(32):
			Mult = Mult + f'Mult #(length) Mult_u{i}(.Muld(IS{i}), .Mulr(W{i}), .result(Mult_result{i});\n'

		head = '''module Mult_array
	#(parameter length = 16)
	(\n'''
		with open('test.v', 'w') as f:
			f.write(input_all)
			f.write(Mult)

	def parallel_compressor_generate(self):
		add = f''
		for i in range(32):
			add = add + f'Mult_result{i}+'	
		with open('test.v', 'w') as f:
			f.write(add)


	def IS_reg(self):
		always = f''
		for i in range(1,32):
			always = always+f'always @(posedge clk or negedge rst_n) begin\n\tif (~rst_n)begin\n\t\tIS{i} <= 0;\n\tend\n\telse begin\n\t\tIS{i} <= IS{i-1};\n\tend\nend\n\n'
		with open('test.v', 'w') as f:
			f.write(always)


	def backforward_coe_update(self):
		head_1 = f'coe_upgrade\n\t#(length)\n\t'
		head_2 = f'\n\t(\n\t\t.clk(clk),\n\t\t.rst_n(rst_n),\n\t\t.error_distance(error_distance),\n\t\t'
		module = f''
		for i in range(32):
			module = module + head_1 + f'backforward_coe_update_u{i}' + head_2 + f'.IS(IS{i}),\n\t\t.W(W{i})\n\t);\n\n'

		with open('test.v', 'w') as f:
			f.write(module)

a = rtl_generate();
a.backforward_coe_update();

